package com.math_space;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ListSort {

    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        Student s1 = new Student();
        s1.setAge(20);
        s1.setName("葛大");
        Student s2 = new Student();
        s2.setAge(19);
        s2.setName("张杰");
        Student s3 = new Student();
        s3.setAge(21);
        s3.setName("宝爷");
        list.add(s1);
        list.add(s2);
        list.add(s3);
        System.out.println(list);

        //排序
        Collections.sort(list, new Comparator<Student>() {
            //> 是升序 ， < 小于是降序
            @Override
            public int compare(Student o1, Student o2) {
                /*if(o1.getAge() > o2.getAge()){
                    return 1;
                }else if(o1.getAge() == o2.getAge()){
                    return 0;
                }
                return -1;*/

                //return o1.getAge()-o2.getAge(); //前减后，升序；
                //return o2.getAge()-o1.getAge(); //反之；
                return o1.getName().compareTo(o2.getName()); //按照姓名升序

            }
        });

        System.out.println(list);
    }
}
